package cn.jekun.shop.app.action;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.shop.common.service.ShopOrderService;
import net.sf.json.JSONObject;

/**
 * 商家订单管理
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class ShopOrderAction extends AppBaseAction{
	@Resource(name = ShopOrderService.BEAN_NAME)
	protected ShopOrderService shopOrderService;
	private Logger logger = Logger.getLogger(ShopOrderAction.class);
	/**
	 * 获取商家订单列表
	 * @return
	 */
	public String getOrders(){
		JSONObject jsonObject = null;
		try {
			logger.info("=========================ShopOrderAction getOrders begin================== ");
			String shopId = this.request.getParameter("shopId");//商家代码
			String status = this.request.getParameter("status");//订单状态
			String orderType = this.request.getParameter("orderType");//订单类型
			String businessType = this.request.getParameter("businessType");//业务类型
			String currentPage = this.request.getParameter("currentPage");//当前页
			String pageSize = this.request.getParameter("pageSize");//页大小
			String days = this.request.getParameter("days");//订单时间
			String memberId = this.request.getParameter("memberId");//会员代码
			this.validateSign(this.getUserId(),this.getLoginSign(),shopId,status,orderType,businessType,currentPage,pageSize,days,memberId);
			this.validateLoginApp();
			Date endDate = null;
			if(days!=null && !days.equals("0") && !"".equals(days)){
				int d = Integer.parseInt(days);
				endDate = DateUtils.addDays(new Date(System.currentTimeMillis()), d*-1);
			}
			String sourceTxt ="shopId=" +shopId+":stataus="+status+":orderType="+orderType+":businessType="+businessType+":currentPage="+currentPage+":memberId="+memberId;
			logger.info(sourceTxt);
			jsonObject = shopOrderService.getOrders(shopId, status, orderType, businessType, currentPage, pageSize, endDate,memberId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
	    logger.info("===========================jsonObject = "+ jsonObject);
		return this.writeJson(jsonObject);// 输出
	}
	/**
	 * 订单操作
	 * @return
	 */
	public String processOrder(){
		JSONObject jsonObject = null;
		try{
			String orderId = this.request.getParameter("orderId");
			String status = this.request.getParameter("status");//订单状态 0 全部，1 待发货，2 待收货，3 成功，4 关闭
			String memo = request.getParameter("memo");//备注
			String reason = request.getParameter("reason");//原因编码
			
			this.validateSign(this.getLoginSign(),this.getUserId(),status,orderId);
			this.validateLoginApp();
			jsonObject = shopOrderService.processOrder(this.getUserId(), orderId,status,memo,reason);
			
			String str = "";
			if (Constants.ERP_ORDER_STATUS.SHIPPING.equals(status)) {// 发货
				str = "发货";
			} else if (Constants.ERP_ORDER_STATUS.CLOSED.equals(status)) {// 取消
				str = "取消订单";
			} else if (Constants.ERP_ORDER_STATUS.OK.equals(status)) {// 完成
				str = "完成订单";
			}
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家处理订单"+orderId+":"+str);//记录操作日志
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);// 输出
	}
	/**
	 * 商家订单详细
	 * @return
	 */
	public String shopOrderDetail(){
		JSONObject jsonObject = null;
		try{
			String orderId = this.request.getParameter("orderId");
			this.validateSign(this.getLoginSign(),orderId);
			this.validateLoginApp();
			jsonObject = shopOrderService.getOrderDetail(orderId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);// 输出
	}
	/**
	 * 订单状态
	 * @return
	 */
	public String getOrderStatusList(){
		JSONObject jsonObject = null;
		try {
			this.validateLoginApp();
			jsonObject = shopOrderService.getOrderStatusList();
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);// 输出
	}
	/**
	 * 订单类型
	 * @return
	 */
	public String getOrderTypeList(){
		JSONObject jsonObject = null;
		try {
			this.validateLoginApp();
			jsonObject = shopOrderService.getOrderTypeList();
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);// 输出
		}
	/**
	 * 订单业务类型
	 * @return
	 */
	public String getBusinessTypeList(){
		JSONObject jsonObject = null;
		try {
			this.validateLoginApp();
			jsonObject = shopOrderService.getBusinessTypeList();
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);// 输出
	}
	/**
	 * 获取商家各种订单状态数量
	 * @return
	 */
	public String getOrderMessageList(){
		JSONObject jsonObject = null;
		try {
			String shopId = this.request.getParameter("shopId");
			this.validateLoginApp();
			this.validateSign(this.getLoginSign(),this.getUserId(),shopId);
			jsonObject = shopOrderService.getOrderMessageList(shopId);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);// 输出
	}
	
//	public String printOrder(){
//		JSONObject jsonObject = null;
//		try {
//			String orderId = this.request.getParameter("orderId");
//			this.validateSign(this.getLoginSign(),this.getUserId(),orderId);
//			this.validateLoginApp();
//			jsonObject = shopOrderService.printOrder(orderId);
//		} catch (BusinessException e) {
//			jsonObject = JSONObject.fromObject(e.getErrorMsg());
//		} catch (Exception e) {
//			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
//			e.printStackTrace();
//		}
//		return this.writeJson(jsonObject);// 输出
//	}

}
